package tutorial.lite_mod.chat_content;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import public_util.model.Const4PublicDataBase;
import public_util.model.PublicDataAdminUser;
import public_util.model.PublicMiddleTable;
import public_util.model.ReturnData;
import public_util.pack_4_stable.tools.DateTool;
import system_api_4_trade.dao.ApiPublicMiddleTableMapper;
import system_api_4_trade.util.sql.InsertTool;
import system_api_4_trade.util.tool_4_handle.DateToolApi;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 插入的时候自动回复
 *
 */
public class TemplateUtilApi {

    private static Logger logger = Logger.getLogger(TemplateUtilApi.class);



    /**
     * 验证链接: duzikai_api_mgr_public_middle_table?invoke_method=public_insert_or_update_middle_table&pre_db_para_middle_table_name=public_middle_table_lite_mod_chat_content&table_status_father=101&type_middle_app=10001&type_inner_app=1001&table_id_father=3&is_online=0&mid_table_remark_005=2018&mid_table_remark_006=201809&mid_table_remark_007=20180905&mid_table_remark_008=985574455&mid_table_remark_011=2326&mid_table_remark_012=tiantian&tag_mid_table=我是人&pre_cmd_para_auto_answer=true
     * 工具零件方法
     * 组装方法: 主要是在before活着after方法里面调用
     * 插入的时候自动回复
     * @return
     */
    public static ReturnData pre_cmd_para_auto_answer(HttpServletRequest request, SqlSession session, HashMap<String, Object> params2db, ReturnData resultData)
    {

        ApiPublicMiddleTableMapper middleTableMapper = session.getMapper(ApiPublicMiddleTableMapper.class);
        String pre_cmd_para_auto_answer = request.getParameter("pre_cmd_para_auto_answer");
        if ("true".equals(pre_cmd_para_auto_answer)) {

            HashMap<String, Object> returnContent = resultData.getReturnData();
            List<PublicMiddleTable> listData =  (List<PublicMiddleTable>)returnContent.get(Const4PublicDataBase.PARA_page_content);
            if (listData == null || listData.size() == 0)
            {
                logger.info("没有pre_cmd_para_auto_answer需要的数据");
                return null;
            }
            PublicMiddleTable insetData = listData.get(0);
            //插入数据即可
            HashMap<String, Object> paraPrice = new HashMap<>();
            paraPrice.put("table_status_father", "101");
            paraPrice.put("table_id_father", insetData.getTable_id_father());
            paraPrice.put("tag_mid_table", "欢迎来到社区邻居！请扫码下放二维码,添加好友,私聊咨询!");
            paraPrice.put("mid_table_remark_005", DateToolApi.format(new Date(), "yyyy"));
            paraPrice.put("mid_table_remark_006", DateToolApi.format(new Date(), "yyyyMM"));
            paraPrice.put("mid_table_remark_007", DateToolApi.format(new Date(), "yyyyMMdd"));
            paraPrice.put("mid_table_remark_008", System.currentTimeMillis());
            paraPrice.put("mid_table_remark_011", "1312");
            paraPrice.put("mid_table_remark_012", "客服001");
            insert_contact_table(session, middleTableMapper, paraPrice);
        }
        return null;
    }



    /**
     * 插入table
     */
    public static void insert_contact_table(SqlSession session , ApiPublicMiddleTableMapper midMapper, HashMap<String, Object> paraPrice)
    {
        paraPrice.put(Const4PublicDataBase.PARA_type_middle_app, 10001);
        paraPrice.put(Const4PublicDataBase.PARA_type_inner_app, 1001);
        paraPrice.put(Const4PublicDataBase.PARA_is_online, 0);
        paraPrice.put(Const4PublicDataBase.PARA_time_insert, new Date());
        HashMap<String, Object> selMap = InsertTool.paramap2Insert(paraPrice, "public_middle_table_lite_mod_chat_content");
        midMapper.insertPublicMiddleTable(selMap);
        session.commit();
    }


}
